package handler

import (
	"blog_go/internal/model"
	"blog_go/internal/service"
	"blog_go/pkg/helper/resp"
	"net/http"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ArticleCategoryHandler interface {
	QueryArticleCategory(ctx *gin.Context)
	AddArticleCategory(ctx *gin.Context)
	UpdateArticleCategory(ctx *gin.Context)
	DelArticleCategory(ctx *gin.Context)
}

type articleCategoryHandler struct {
	*Handler
	articleCategoryService service.ArticleCategoryService
}

func NewArticleCategoryHandler(handler *Handler, articleCategoryService service.ArticleCategoryService) ArticleCategoryHandler {
	return &articleCategoryHandler{
		Handler:                handler,
		articleCategoryService: articleCategoryService,
	}
}

// 查询所有的文章分类
func (h *articleCategoryHandler) QueryArticleCategory(ctx *gin.Context) {
	data, err := h.articleCategoryService.QueryArticleCategory()

	if err != nil {
		h.logger.Error("QueryArticleCategory failed", zap.Any("error", err))
		resp.HandleError(ctx, http.StatusInternalServerError, 1, err.Error(), nil)
		return
	}

	resp.HandleSuccess(ctx, data)
}

// 新增文章分类
func (h *articleCategoryHandler) AddArticleCategory(ctx *gin.Context) {
	var params struct {
		CategoryName string `json:"categoryName" binding:"required"`
	}

	if err := ctx.ShouldBind(&params); err != nil {
		h.logger.Warn("AddArticleCategory", zap.Any("error", err))
		resp.HandleError(ctx, http.StatusBadRequest, 1, err.Error(), nil)
		return
	}

	h.logger.Info("AddArticleCategory attempted", zap.Any("categoryName", params.CategoryName))

	err := h.articleCategoryService.AddArticleCategory(params.CategoryName)

	if err != nil {
		h.logger.Error("AddArticleCategory failed", zap.Any("categoryName", params.CategoryName), zap.Any("error", err))
		resp.HandleError(ctx, http.StatusInternalServerError, 1, err.Error(), nil)
		return
	}

	resp.HandleSuccess(ctx, nil)
}

// 编辑文章分类
func (h *articleCategoryHandler) UpdateArticleCategory(ctx *gin.Context) {
	var articleCategory model.ArticleCategory

	if err := ctx.ShouldBind(&articleCategory); err != nil {
		h.logger.Warn("UpdateArticleCategory", zap.Any("error", err))
		resp.HandleError(ctx, http.StatusBadRequest, 1, err.Error(), nil)
		return
	}

	h.logger.Info("UpdateArticleCategory attempted", zap.Any("articleCategory", articleCategory))

	err := h.articleCategoryService.UpdateArticleCategory(articleCategory)

	if err != nil {
		h.logger.Error("UpdateArticleCategory failed", zap.Any("articleCategory", articleCategory), zap.Any("error", err))
		resp.HandleError(ctx, http.StatusInternalServerError, 1, err.Error(), nil)
		return
	}

	resp.HandleSuccess(ctx, nil)
}

// 删除文章分类
func (h *articleCategoryHandler) DelArticleCategory(ctx *gin.Context) {
	var params struct {
		CategoryId int `json:"categoryId" binding:"required"`
	}

	if err := ctx.ShouldBind(&params); err != nil {
		h.logger.Warn("DelArticleCategory", zap.Any("error", err))
		resp.HandleError(ctx, http.StatusBadRequest, 1, err.Error(), nil)
		return
	}

	h.logger.Info("DelArticleCategory attempted", zap.Any("categoryId", params.CategoryId))

	err := h.articleCategoryService.DelArticleCategory(params.CategoryId)

	if err != nil {
		h.logger.Error("DelArticleCategory failed", zap.Any("categoryId", params.CategoryId), zap.Any("error", err))
		resp.HandleError(ctx, http.StatusInternalServerError, 1, err.Error(), nil)
		return
	}

	resp.HandleSuccess(ctx, nil)
}
